package org.cuatrovientos.erp.dao;

import java.util.List;

import org.cuatrovientos.erp.model.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Enrico Baroni
 * <User>'s DAO.
 */
public class UserDAO implements GenericInterfaceDAO {

	@Autowired
	 private SessionFactory sessionFactory;
	
	/**
	 * Get list's <User>
	 */
	@Override
	public List<User> selectAll() {

	    List<User> users = getSession().createQuery("from user").list();
	    return users;
	}

	/**
	 * Get an <User>
	 */
	@Override
	public User selectById(int id) {
		User user = (User) getSession().get(User.class, id);
	    return user;
	}

	/**
	 * delete an <User>
	 */
	@Override
	public void delete(int id) {
		User user = selectById(id);
		getSession().delete(user);
		
	}

	/**
	 * Insert an <User>
	 */
	@Override
	public void insert(Object newUser) {
		 Integer id = (Integer) getSession().save(newUser);
		    ((User) newUser).setId(id);  
		
	}

	/**
	 * update an <User>
	 */
	@Override
	public void update(Object user) {
		getSession().merge(user);
		
	}

	/**
	 * Get a session.
	 */
	@Override
	public Session getSession() {
		 return sessionFactory.getCurrentSession();
	}

	

}
